﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mindfor.Web.Models
{
	/// <summary>
	/// Contains information about user login and domain.
	/// </summary>
	public class DomainIdentity
	{
		/// <summary>
		/// Gets or sets login.
		/// </summary>
		public string Login { get; set; }

		/// <summary>
		/// Gets or sets domain.
		/// </summary>
		public string Domain { get; set; }

		/// <summary>
		/// Initializes new instance.
		/// </summary>
		/// <param name="login">User login.</param>
		/// <param name="domain">User domain.</param>
		public DomainIdentity(string login, string domain)
		{
			Login = login;
			Domain = domain;
		}

		/// <summary>
		/// Parses login and domain from identity string like "Domin\Login".
		/// </summary>
		/// <param name="identity">User identity.</param>
		public DomainIdentity(string identity)
		{
			Login = identity;
			
			int i = identity.IndexOf('\\');
			if (i != -1)
			{
				Domain = identity.Substring(0, i);
				Login = identity.Substring(i + 1);
			}
		}

		/// <summary>
		/// Returns string representation like "Domain\Login";
		/// </summary>
		public override string ToString()
		{
			if (String.IsNullOrEmpty(Domain))
				return Login;
			return Domain + "\\" + Login;
		}
	}
}
